package com.mlnx.work.service.iml;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlnx.common.form.PageForm;
import com.mlnx.work.dao.mysql.RoleMapper;
import com.mlnx.work.entity.Role;
import com.mlnx.work.entity.RoleRight;
import com.mlnx.work.enums.ResponseEnum;
import com.mlnx.work.exception.WorkServiceException;
import com.mlnx.work.pojo.form.RoleFilterForm;
import com.mlnx.work.pojo.form.RoleForm;
import com.mlnx.work.service.RoleRightService;
import com.mlnx.work.service.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * author name: amanda.shan
 * create time: 2020-12-28 17:09:55
 */
@Service
public class RoleServiceIml extends BaseService<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleRightService roleRightService;

    @Autowired
    @Override
    public void setBaseMapper(RoleMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }


    @Override
    public IPage page(PageForm pageForm, Object filter) throws IllegalAccessException {
        return baseMapper.listRoleVoPage(new Page<>(pageForm.getCurrent(), pageForm.getSize()), (RoleFilterForm) filter);
    }

    @Override
    public List<Object> list(Object filter) throws IllegalAccessException {
        return baseMapper.listAll();
    }

    @Transactional
    @Override
    public void save(RoleForm roleForm) {
        Role role = new Role();
        role.setRoleKey(roleForm.getRoleKey());
        role.setRoleName(roleForm.getRoleName());
        role.setDescription(roleForm.getDescription());
        role.setMenus(JSON.toJSONString(roleForm.getMenus()));

        checkExist(role);

        save(role);

        if (roleForm.getRightIds() != null) {
            List<RoleRight> roleRights = new ArrayList<>();
            for (Integer rightId : roleForm.getRightIds()) {
                RoleRight roleRight = new RoleRight();
                roleRight.setRoleId(role.getId());
                roleRight.setRightId(rightId);
                roleRights.add(roleRight);
            }
            roleRightService.saveBatch(RoleRight.class, roleRights);
        }
    }

    @Transactional
    @Override
    public void updateById(Integer id, RoleForm roleForm) {

        Role role = new Role();
        role.setId(id);
        role.setRoleName(roleForm.getRoleName());
        role.setDescription(roleForm.getDescription());
        role.setMenus(JSON.toJSONString(roleForm.getMenus()));

        checkExist(role);

        updateById(role);

        if (roleForm.getRightIds() != null) {

            roleRightService.remove(new QueryWrapper<RoleRight>().eq("role_id", role.getId()));

            List<RoleRight> roleRights = new ArrayList<>();
            for (Integer rightId : roleForm.getRightIds()) {
                RoleRight roleRight = new RoleRight();
                roleRight.setRoleId(role.getId());
                roleRight.setRightId(rightId);
                roleRights.add(roleRight);
            }
            roleRightService.saveBatch(RoleRight.class, roleRights);
        }
    }

    private void checkExist(Role role) {

        if (role.getId() != null) {
            Role role1 = getById(role.getId());
            if (role1.getRoleName().equals(role.getRoleName())) {
                return;
            }
        }

        if (role.getRoleName() != null) {
            List<Role> roles = list(new QueryWrapper<Role>().eq("role_name", role.getRoleName()));
            if (roles != null && roles.size() > 0) {
                throw new WorkServiceException(ResponseEnum.ROLE_FAIL);
            }
        }
    }
}

